<template>
  <div class="container">
    <h2>头条登录</h2>
    <el-form :model="ruleForm" :rules="rules" class="login_form" ref="form">
      <el-form-item prop="mobile">
        <el-input v-model="ruleForm.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item  prop="code">
        <el-input v-model="ruleForm.code" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item prop="checked">
          <el-checkbox v-model="ruleForm.checked">我已阅读并同意用户协议和隐私条款</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onLogin" :loading="isLoading">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {login} from '../../api/user';
export default {
  name: 'Login',
  created() {},
  data() {
    return {
      ruleForm:{
        mobile:'', //手机号
        code:'', //验证码
        checked:false //复选框
      },
      rules:{
         mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            // { , message: '长度在 3 到 5 个字符', trigger: 'blur' }
            { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '手机号格式错误',trigger: 'change' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            // {}
            { pattern: /^\d{6}$/, message: '验证码格式错误',trigger: 'change' }
          ],
          checked:[
            // value为验证的值   成功返回callback()
            {validator:(rule, value, callback)=>{
              // console.log(value);
              if(value){
                callback()
              }else{
                callback('请同意用户协议')
              }
            },trigger: 'change'}
          ]
      },
      isLoading:false  //登录加载中
    };
  },
  props: {},
  methods: {
    async onLogin(){
        // 手动进行表单验证  如果表单验证通过 发送请求获取用户信息
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          // 表单验证
          // if(this.ruleForm.checked){
            try {
              this.isLoading=true
              const {data}=await login({mobile:this.ruleForm.mobile,code:this.ruleForm.code})
              // console.log(data);
              window.localStorage.setItem('user',JSON.stringify(data.data))
              this.$message.success('登陆成功');
              this.$router.replace({
                name:'home',
              })
              this.isLoading=false
            } catch (error) {
              this.$message.error('手机号或密码错误');
              this.isLoading=false
            }
          // }else{
          //   this.$message.error('请勾选');
          // }
        } else {
          // console.log('error submit!!');
          return false;
        }
      })

    }
  },
};
</script>

<style lang="less" scoped>
.container{
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
   background: url('./th1.png') no-repeat fixed;
  background-size: cover;
  h2{
    position: absolute;
    top: 225px;
    color: #2e95ff;
  }
  .login_form{
    background-color: #fff;
    padding: 50px;
    padding-bottom: 20px;
    min-width: 300px;
    .el-button{
      width: 100%;
    }
  }
}
html{

}
</style>
